<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<jsp:include page="../../include/top.jsp"/>
<script type="text/javascript">
    var editorIntro;

    KindEditor.options.filterMode = false;

    KindEditor.ready(function (K) {
        editorIntro = K.create('textarea[name="description"]', {
            filterMode: true,
            htmlTags: {
                span: ['.color'],
                p: [],
                strong: [],
                a: ["href", "target"],
                //br: [],
                img: ["src", "width", "height"],
                input: ['onclick', 'click', 'type', 'value', 'url', 'data-toggle', 'href', 'id', 'name', 'alt', 'data-ke-onclick', 'href', '/'],
                embed: ['src', 'name', 'id', 'width', 'height', 'type', 'loop', 'autostart', 'quality', 'style', 'align', 'allowscriptaccess', 'flashvars', 'allowfullscreen', '/'],
                vote: ['vote', '/'],
                video: ['id', 'name', 'src', 'controls', 'width', 'height']
            },
            width:"80%",
            height:"300px",
            newlineTag: "p",
            uploadJson: '${BasePath}/base/image/upload',
            items: ['undo', 'redo', 'image', 'multiimage', 'forecolor', 'bold', 'link', 'source', 'preview', 'fullscreen'],
            afterChange: function () {
                this.sync();
            }
        });
    });
</script>
<div class="main_body">
    <div class="span9 page_message">
        <section id="contents">
            <ul class="tab-nav nav">
                <li><a href="${BasePath}/order/product/list?${PublicParams}">产品列表<span
                        class="arrow fa fa-sort-up"></span></a></li>
                <li class="current"><a href="">产品添加/修改<span class="arrow fa fa-sort-up"></span></a></li>
                <li>
                    <a href="${BasePath}/order/product/toProductInstitution?${PublicParams}">批量更新产品适用机构<span class="arrow fa fa-sort-up"></span></a>
                </li>
            </ul>
            <div class="tab-content">
                <div id="tab1" class="tab-pane in tab1">
                    <!-- 表单 -->
                    <form class="form-horizontal" name="myForm" id="myForm" action="${BasePath}/order/product/save" method="post">
                        <input type="hidden" name="id" value="${bean.id }"/>
                        <input type="hidden" name="type" value="${type }"/>
                        <input type="hidden" name="itemId" value="${itemId }"/>
                        <div class="form-item cf toggle-idInstitution">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                机构名称：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <select id="idInstitution" name="idInstitution">
                                    <c:forEach items="${institutionList}" var="inst">
                                        <option value="${inst.id}">${inst.name}</option>
                                    </c:forEach>
                                </select>
                                <input class="input-xlarge focused" id="keyword" name="keyword" type="text">
                                <button class="btn" id="search" onclick="searchInstitution()" type="button">搜索</button>
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div class="form-item cf toggle-name float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                名称：
                                <span class="check-tips"></span>
                            </label>
                            <div class="controls">
                                <input class="input-xlarge focused" id="name" name="name" type="text" value="${bean.name }">
                            </div>
                        </div>
                        <div class="form-item cf toggle-idProductType float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                产品分类：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <select id="productTypeId" name="productTypeId">
                                    <c:forEach items="${productTypeList}" var="pt">
                                        <option value="${pt.id}">${pt.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div class="form-item cf toggle-price float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                产品价格：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <input class="input-xlarge focused" id="price" name="price" type="text" value="${bean.price }">
                            </div>
                        </div>
                        <div class="form-item cf toggle-salePrice float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                销售价格：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <input class="input-xlarge focused" id="salePrice" name="salePrice"
                                       type="text" value="${bean.salePrice }">
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div class="form-item cf toggle-basePrice float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                底价：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <input class="input-xlarge focused" id="basePrice" name="basePrice"
                                       type="text" value="${bean.basePrice }">
                            </div>
                        </div>
                        <div class="form-item cf toggle-basePrice float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                代理价：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <input class="input-xlarge focused" id="agentPrice" name="agentPrice" type="text" value="${bean.agentPrice }">
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div class="form-item cf toggle-onsale float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                是否在售：
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <select id="onsale" name="onsale">
                                    <option value="1" <c:if test="${bean.onsale==1}">selected</c:if>>是</option>
                                    <option value="0" <c:if test="${bean.onsale==0}">selected</c:if>>否</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-item cf toggle-isHot float-left">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                推荐：用在网站上
                                <span class="check-tips"></span>
                            </label>
                            <div class="controls">
                                <select id="isPublic" name="isPublic">
                                    <option value="0"  <c:if test="${bean.isPublic=='0'}">selected</c:if>>不推荐</option>
                                    <option value="1" <c:if test="${bean.isPublic=='1'}">selected</c:if>>推荐</option>
                                </select>
                            </div>
                        </div>
                        <div class="clear"></div>
                        <div class="form-item cf toggle-name">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                服务描述：（暂时用于打印在口腔卡上的服务项目）
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <label class="textarea input-large">
                                    <textarea name="service" id="service" style="width:500px"></textarea>
                                </label>
                            </div>
                        </div>
                        <div class="form-item cf toggle-name">
                            <label class="item-label">
                                <span class="need_flag">*</span>
                                图片:&nbsp;
                                <span class="check-tips"></span>
                            </label>
                            <div class="controls">
                                <input type="hidden" name="imageFile" value="${bean.imageFile}" id="imageFile"/>
                                <div class="upload-img-box">
                                    <c:forEach items="${bean.imageFile.split(\";\")}" var="v">
                                        <c:if test="${v != \"\"}">
                                            <div class="upload-pre-item2"><img width="180" height="100" src="${v}"/><input type="hidden" name="hideImages" value="${v}"/><input type="button" class="imageDelete btn btn-small" onclick="deleteImage(this)" value="删除"/></div>
                                        </c:if>
                                    </c:forEach>
                                </div>
                                <div class="uploadrow2" title="点击修改图片">
                                    <input type="file" name="file" id="upload_picture_cover"/>
                                    <input type="hidden" name="cover" id="cover_id_cover"/>
                                </div>
                                <script type="text/javascript">
                                    $(function () {
                                        /* 初始化上传插件 */
                                        $("#upload_picture_cover").uploadify({
                                            "height": 100,
                                            "swf": "${BasePath}/Public/static/uploadify/uploadify.swf",
                                            "fileObjName": "file",
                                            "buttonText": "上传图片",
                                            "uploader": "${BasePath}/base/image/upload;jsessionid=<%=session.getId()%>",
                                            "width": 180,
                                            'removeTimeout': 1,
                                            'fileTypeExts': '*.jpg; *.png; *.gif;',
                                            'onUploadSuccess': uploadPicturecover
                                        });
                                        function uploadPicturecover(file, data) {
                                            var data = $.parseJSON(data);
                                            var src = '';
                                            if (data.errno) {
                                                src = data.url || '' + data.path;
                                                $("#cover_id_cover").parent().parent().find('.upload-img-box').append(
                                                    '<div class="upload-pre-item2"><img width="180" height="100" src="' + src + '"/><input type="hidden" name="hideImages" value="' + data.uri +'"/><input type="button" class="imageDelete btn btn-small" onclick="deleteImage(this)" value="删除"/></div>'
                                                );
                                                <%--$('.weixin-cover-pic').attr('src', "${BasePath}" + src);--%>
                                                if($("#imageFile").val() == ""){
                                                    $("#imageFile").val(data.uri);
                                                }else {
                                                    $("#imageFile").val($("#imageFile").val() + ";" + data.uri);
                                                }
                                            } else {
                                                updateAlert(data.errdesc);
                                                setTimeout(function () {
                                                    $('#top-alert').find('button').click();
                                                    $(that).removeClass('disabled').prop('disabled', false);
                                                }, 1500);
                                            }
                                        }
                                    });

                                    function deleteImage(obj){
                                        $(obj).parent().remove();
                                        var objs = $("input[name=hideImages]");
                                        var arr = [];
                                        for(i=0;i<objs.length;i++){
                                            arr.push($(objs[i]).val());
                                        }
                                        $("#imageFile").val(arr.join(";"));
                                    }
                                </script>
                            </div>
                        </div>
                        <div class="form-item cf toggle-content">
                            <label class="item-label">
                                内容
                                <span class="check-tips"></span>
                            </label>

                            <div class="controls">
                                <label class="textarea">
                                    <textarea name="description">${bean.description}</textarea>
                                </label>
                            </div>
                        </div>
                        <div class="form-item cf">
                            <button class="btn submit-btn" id="submit" onclick="checkForm()" type="button">确 定</button>
                        </div>
                        <%--<button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal" redirect-url="${BasePath}/order/product/list?${PublicParams}">确 定--%>
                        <%--</button>--%>
                    </form>
                </div>
            </div>
        </section>
    </div>
</div>
<jsp:include page="../../include/bottom.jsp"/>
<script>
    function searchInstitution() {
        var keyword = $("#keyword").val();
        $("#idInstitution option").remove();
        $.post("${BasePath}/order/institution/getJSON", {keyword: keyword}, function (data) {
            $.each(data.data, function (InfoIndex, Info) {
                $("#idInstitution").append('<option value=' + Info.id + '>' + Info.name + '</option>');
            });
        });
    }


    function checkForm() {
        var name = $("#name").val();
        if (name.trim() == "") {
            layer.msg('产品名称不能为空!', {icon: 5});
            return false;
        }
        var price = $("#price").val();
        if (price.trim() == "") {
            layer.msg('产品价格不能为空!', {icon: 5});
            return false;
        }
        var salePrice = $("#salePrice").val();
        if (salePrice.trim() == "") {
            layer.msg('销售价格不能为空!', {icon: 5});
            return false;
        }
        var basePrice = $("#basePrice").val();
        if (basePrice.trim() == "") {
            layer.msg('产品底价不能为空!', {icon: 5});
            return false;
        }
        var agentPrice = $("#agentPrice").val();
        if (agentPrice.trim() == "") {
            layer.msg('代理价不能为空!', {icon: 5});
            return false;
        }
        $('#myForm').ajaxSubmit(function (data) {
            if (data.errno == 1) {
                layer.msg(data.errdesc, {icon: 5});
            } else {
                layer.msg('操作成功！');
                window.location.href = "${BasePath}/order/product/list?${PublicParams}";
            }
        });
    }
</script>